﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Security.Principal;

/// <summary>
/// Summary description for PageBase
/// </summary>
public class PageBase : Page
{
    private User currentUser;
    protected AuthenticationBLL AuthManager { set; get; }
    protected UserBLL UserManager { set; get; }
    protected RoleDAL RoleManager { set; get; }
    private UserPrincipal principalUser;

    protected override void OnInit(EventArgs e)
    {
        UserManager = new UserBLL();
        RoleManager = new RoleDAL();

        base.OnInit(e);
    }

    protected override void OnLoad(EventArgs e)
    {

        UpdateCurrentUserInfo();

        base.OnLoad(e);
    }

    public UserPrincipal User
    {
        get
        {
            try
            {
                GenericPrincipal nowUser = (GenericPrincipal)Page.User;
                principalUser = UserPrincipal.GetUserPrincipal(nowUser);
            }
            catch(Exception ex)
            {
                Response.Redirect("~/Account/Login.aspx", true);
            }
            return principalUser;
        }
    }

    public User CurrentUser
    {
        get
        {
            if (currentUser == null)
            {
                currentUser = SessionHelper.UserInfo;
            }

            return currentUser;
        }
        set
        {
            currentUser = value;
        }
    }

    private void UpdateCurrentUserInfo()
    {
        if (this.Request.IsAuthenticated)
        {
            bool userFound = false;
            DataResponse<User> userResponse = AuthManager.GetUser(User.Identity.Name);

            if (userResponse != null)
            {
                if (string.IsNullOrEmpty(userResponse.ErrorCode))
                {
                    if (userResponse.Result != null)
                    {
                        SessionHelper.UserInfo = userResponse.Result;
                        userFound = true;
                    }
                }
            }
            if (!userFound)
            {
                SessionHelper.UserInfo = null;
                Response.Redirect("~/Account/Login.aspx");
            }
        }
        else
        {
            if (string.Compare(Helper.Common.GetPageName(Page), "login.aspx",true) != 0)
            {
                //Response.Redirect("~/Account/Login.aspx");
            }
        }
    }

    public bool UserHasAnyRole(string[] roles)
    {
        bool hasRole = false;
        foreach (string role in roles)
        {
            if (string.Compare(role, CurrentUser.UserRole.Name, true) == 0)
            {
                hasRole = true;
                break;
            }
        }
        return hasRole;
    }

    public void ConfigureUserRoleAccess(string[] roles)
    {
        bool roleHasAccess = false;
        if (CurrentUser != null)
        {
            if (CurrentUser.UserRole != null)
            {
                foreach (string role in roles)
                {
                    if (string.Compare(role, CurrentUser.UserRole.Name, true) == 0)
                    {
                        roleHasAccess = true;
                        break;
                    }
                }
            }
        }
        if (!roleHasAccess)
        {
            Response.Redirect("~/Account/404.aspx");
        }
    }

	public PageBase()
	{
        AuthManager = new AuthenticationBLL();
	}
}